package br.com.sga.helper;

import br.com.sga.util.conexao.Pool;
import br.com.sga.util.conexao.PoolImpl;
import br.com.sga.erro.GeralException;
import br.com.sga.modelo.dao.UsuarioDaoImpl;
import br.com.sga.modelo.regra.Regra;
import br.com.sga.modelo.regra.RUsuarioLogin;
import br.com.sga.modelo.regra.RViewLogin;
import br.com.sga.modelo.regra.RViewMain;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author kenzo
 */
public class Helper {

    private Map<String, Regra> mapa;
    private Pool pool;

    public Helper() {
        super();
        this.pool = PoolImpl.getInstance();
        mapa = new HashMap<>();
        mapa.put("", new RViewMain());
        mapa.put(null, new RViewMain());
        mapa.put("tusuario_login", new RViewLogin());
        mapa.put("usuario_login", new RUsuarioLogin(new UsuarioDaoImpl(pool)));
    }

    public String executar(HttpServletRequest request) {
        String acao = request.getParameter("acao");
        Regra regra = mapa.get(acao);
        try {
            return regra.executar(request);
        } catch (GeralException e) {
            request.setAttribute("erro", new GeralException("Não foi possível executar a ação: " + acao, e.getCause()));
            return "./jsp/erro.jsp";
        }
    }
}
